using System;
using System.Reflection;

using Pegasus.Diagnostics;

namespace Pegasus.Workflow.Service
{
	/// <summary>
	/// This object is used to catch the events comming from the host application and route them
	/// to the proper workflow context object.
	/// </summary>
	internal class DataExchangeProxy
	{
		// Local Instance Values
		private WorkflowService m_workflowService = null;
		private Type m_dataExchangeType = null;
		private EventInfo m_eventInfo = null;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:WorkflowEventHandler"/> class.
		/// </summary>
		public DataExchangeProxy()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:WorkflowEventHandler"/> class.
		/// </summary>
		/// <param name="workflowService">The workflow service.</param>
		/// <param name="dataExchangeType">Type of the data exchange.</param>
		/// <param name="eventInfo">The event info.</param>
		public DataExchangeProxy( WorkflowService workflowService, Type dataExchangeType, EventInfo eventInfo )
		{
			// Check Parameters
			ParamCode.AssertNotNull( workflowService, "workflowService" );
			ParamCode.AssertNotNull( dataExchangeType, "dataExchangeType" );
			ParamCode.AssertNotNull( eventInfo, "eventInfo" );

			m_workflowService = workflowService;
			m_dataExchangeType = dataExchangeType;
			m_eventInfo = eventInfo;
		}

		/// <summary>
		/// Datas the exchange event handler.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="args">The <see cref="T:Pegasus.Workflow.Service.WorkflowEventArgs"/> instance containing the event data.</param>
		public void DataExchangeEventHandler( object sender, WorkflowEventArgs args )
		{
			// Check Parameters
			ParamCode.AssertNotNull( sender, "sender" );
			ParamCode.AssertNotNull( args, "args" );

			WorkflowMessageService msgService = m_workflowService.GetService<WorkflowMessageService>();
			msgService.SendDataExchangeMessage( m_dataExchangeType, m_eventInfo, sender, args );
		}
	}
}
